package cc.rengu.redp.flowable.controller;


import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.flowable.domain.FormDesign;
import cc.rengu.redp.flowable.service.FormDesignService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("form")
public class FormDesignController extends BaseController {

    @Autowired
    private FormDesignService formDesignService;

    /**
     * 无参数：查询全部   formId：单一查询   name：模糊查询
     * @param queryRequest queryRequest
     * @param formDesign  formDesign
     * @return List<FormDesign>
     */
    @Log("表单设计查询")
    @GetMapping
    public Map<String, Object> formQuery(QueryRequest queryRequest, FormDesign formDesign) {
        return getDataTable(formDesignService.formQuery(formDesign, queryRequest));
    }

    @Log("表单设计查询")
    @GetMapping("/getAll")
    public List<FormDesign> formQuery() {
        return formDesignService.formQueryAll();
    }

    /**
     * 表单设计新增
     *
     * @param formDesign 实体类
     */
    @Log("表单设计新增")
    @PostMapping
    public RedpResponse addForm(FormDesign formDesign) {
        try {
            formDesign.setFormJson(putBackStringForXssFilter(formDesign.getFormJson()));
            this.formDesignService.addForm(formDesign);
        } catch (Exception e) {
            String message = "新增表单失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("新增表单成功");
    }

    /**
     * 表单删除
     * @param formIds 表单Id
     */
    @Log("表单删除")
    @DeleteMapping("/{formIds}")
    public RedpResponse deleteForm(@NotBlank(message = "{required}") @PathVariable String formIds) {
        try {
            String[] ids = formIds.split(StringPool.COMMA);
            this.formDesignService.deleteForm(ids);
        } catch (Exception e) {
            String message = "删除表单失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("删除表单成功");
    }


    @Log("表单修改")
    @PutMapping
    public RedpResponse updateProfile(@Valid FormDesign formDesign) {
        try {
            formDesign.setFormJson(putBackStringForXssFilter(formDesign.getFormJson()));
            this.formDesignService.modifiedForm(formDesign);
        } catch (Exception e) {
            String message = "修改表单失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("修改表单成功");
    }

    /**
     * 预览表单
     * @return String
     */
    @Log("预览表单")
    @GetMapping("/{formId}")
    public FormDesign getForm(@NotBlank(message = "{required}") @PathVariable String formId){
        return formDesignService.getForm(formId);
    }

    /**
     * json中包含xssFilter会自动过滤的关键字select，前端替换为xssSelect后还原
     * @param formJsonString
     * @return
     */
    private static String putBackStringForXssFilter(String formJsonString) {
        return formJsonString.replaceAll("\"type\":\"xssSelect\"", "\"type\":\"select\"")
                .replaceAll("\"icon\":\"icon-xssSelect\"", "\"icon\":\"icon-select\"");
    }
}
